package com.fingard.dsp.bank.directbank.zbf01;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.RefundState;
import com.fingard.dsp.bank.directbank.bankInterface.IRecRefund;
import com.fingard.dsp.bank.reqrespobj.recrefund.ReqQryRecRefund;
import com.fingard.dsp.bank.reqrespobj.recrefund.ReqRecRefund;
import com.fingard.dsp.bank.reqrespobj.recrefund.RetAtsQryRecRefund;
import com.fingard.dsp.bank.reqrespobj.recrefund.RetAtsRecRefund;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;

import java.util.Date;

public class ZBF01RecRefund extends ZBF01Base implements IRecRefund {
    @Override
    public RetAtsRecRefund submitRecRefund(ReqRecRefund p_reqRef) throws Exception {
        Date date = Format.DateTimeFormat.parse(p_reqRef.oriTransDate);
        if (DateHelper.compareDay(date, new Date()) == 0) {
            return submitRecRefundToday(p_reqRef);
        } else {
            return submitRecRefunds(p_reqRef);
        }
    }

    public RetAtsRecRefund submitRecRefunds(ReqRecRefund p_reqRef) throws Exception {
        RetAtsRecRefund retAts = new RetAtsRecRefund();
        retAts.oriRdSeq = p_reqRef.oriRdSeq;
        retAts.refundNo = p_reqRef.refundNo;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqRef.ownAct, "unifiedorder");
            if (actItem != null) {
                JSONObject refundRequest = new JSONObject();
                refundRequest.put("businessCode", "NDR1910001");
                refundRequest.put("mchRefundSn", p_reqRef.refundNo);
                refundRequest.put("amount", StringHelper.amountMultiply100(p_reqRef.amount));
                refundRequest.put("cardNo", p_reqRef.oriPayAct);
                refundRequest.put("payTrsSn", p_reqRef.oriRdSeq);

                WriteBankLogStep2(refundRequest.toJSONString());
                String tmpStrRet = sendToBank("QRPayRefundSubmit", refundRequest, actItem);
                WriteBankLogStep3(tmpStrRet);

                if (tmpStrRet.contains("{")) {
                    JSONObject respJson = JSON.parseObject(tmpStrRet);
                    String tmpRespCode = respJson.getString("retCode");
                    retAts.respCode = "0";
                    retAts.respInfo = respJson.getString("retMsg");

                    String refundStatus = respJson.getString("refundStatus");
                    if ("00".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_SUCCESS;
                    } else if ("01".equalsIgnoreCase(tmpRespCode) || "01".equalsIgnoreCase(refundStatus) || "06".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_FAIL;
                    } else if ("02".equalsIgnoreCase(tmpRespCode) || "02".equalsIgnoreCase(refundStatus) || "03".equalsIgnoreCase(refundStatus) || "04".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_PAYING;
                    } else if ("05".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_CLOSED;
                    }
                    retAts.oriOppOrderNo = respJson.getString("refundTrsSn");
                    if (StringHelper.isNullOrEmpty(retAts.oriOppOrderNo)) {
                        retAts.oriOppOrderNo = respJson.getString("payTrsSn");
                    }
                    retAts.oppRefundNo = respJson.getString("mchRefundSn");
                    retAts.originalCode = refundStatus;
                    retAts.originalInfo = respJson.getString("retMsg");
                    retAts.unifiedCode = getBankConfig().getMapValueByKey("recRefundMapping", tmpRespCode);
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "银行返回报文格式非法!";
                }
            } else {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号[" + p_reqRef.ownAct + "]商户配置信息!";
                retAts.refundState = RefundState.REFUND_FAIL;
                retAts.originalCode = "E9981";
                retAts.originalInfo = retAts.respInfo;
                retAts.unifiedCode = "E9981";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return retAts;
    }

    public RetAtsRecRefund submitRecRefundToday(ReqRecRefund p_reqRef) throws Exception {
        RetAtsRecRefund retAts = new RetAtsRecRefund();
        retAts.oriRdSeq = p_reqRef.oriRdSeq;
        retAts.refundNo = p_reqRef.refundNo;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqRef.ownAct, "unifiedorder");
            if (actItem != null) {
                JSONObject refundRequest = new JSONObject();
                refundRequest.put("businessCode", "DR2003001");
                refundRequest.put("mchRefundSn", p_reqRef.refundNo);
                refundRequest.put("amount", StringHelper.amountMultiply100(p_reqRef.amount));
                refundRequest.put("payTrsSn", p_reqRef.oriRdSeq);

                WriteBankLogStep2(refundRequest.toJSONString());
                String tmpStrRet = sendToBank("QRPayRefundSubmitToday", refundRequest, actItem);
                WriteBankLogStep3(tmpStrRet);

                if (tmpStrRet.contains("{")) {
                    JSONObject respJson = JSON.parseObject(tmpStrRet);
                    String tmpRespCode = respJson.getString("retCode");
                    retAts.respCode = "0";
                    retAts.respInfo = respJson.getString("retMsg");

                    String refundStatus = respJson.getString("refundStatus");
                    if ("00".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_SUCCESS;
                    } else if ("01".equalsIgnoreCase(tmpRespCode) || "01".equalsIgnoreCase(refundStatus) || "06".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_FAIL;
                    } else if ("02".equalsIgnoreCase(refundStatus) || "03".equalsIgnoreCase(refundStatus) || "04".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_PAYING;
                    } else if ("05".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_CLOSED;
                    }
                    retAts.oriOppOrderNo = respJson.getString("refundTrsSn");
                    if (StringHelper.isNullOrEmpty(retAts.oriOppOrderNo)) {
                        retAts.oriOppOrderNo = respJson.getString("payTrsSn");
                    }
                    retAts.oppRefundNo = respJson.getString("mchRefundSn");
                    retAts.originalCode = refundStatus;
                    retAts.originalInfo = respJson.getString("retMsg");
                    retAts.unifiedCode = getBankConfig().getMapValueByKey("recRefundMapping", tmpRespCode);
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "银行返回报文格式非法!";
                }
            } else {
                retAts.respCode = "0";
                retAts.respInfo = "未找到账号[" + p_reqRef.ownAct + "]商户配置信息!";
                retAts.refundState = RefundState.REFUND_FAIL;
                retAts.originalCode = "E9981";
                retAts.originalInfo = retAts.respInfo;
                retAts.unifiedCode = "E9981";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return retAts;
    }

    @Override
    public RetAtsQryRecRefund queryRecRefund(ReqQryRecRefund p_reqQryRef) throws Exception {
        Date date = Format.DateTimeFormat.parse(p_reqQryRef.paySentDate);
        if (DateHelper.compareDay(date, new Date()) == 0) {
            return queryRecRefundToday(p_reqQryRef);
        } else {
            return queryRecRefunds(p_reqQryRef);
        }
    }

    public RetAtsQryRecRefund queryRecRefunds(ReqQryRecRefund p_reqQryRef) throws Exception {
        RetAtsQryRecRefund retAts = new RetAtsQryRecRefund();
        retAts.oriRdSeq = p_reqQryRef.oriRdSeq;
        retAts.refundNo = p_reqQryRef.refundNo;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqQryRef.ownAct, "unifiedorder");
            if (actItem != null) {
                JSONObject refundRequest = new JSONObject();
                refundRequest.put("businessCode", "NDR1910002");
                refundRequest.put("mchRefundSn", p_reqQryRef.refundNo);

                WriteBankLogStep2(refundRequest.toJSONString());
                String tmpStrRet = sendToBank("QRPayRefundQuery", refundRequest, actItem);
                WriteBankLogStep3(tmpStrRet);

                if (tmpStrRet.contains("{")) {
                    JSONObject respJson = JSON.parseObject(tmpStrRet);
                    String tmpRespCode = respJson.getString("retCode");
                    retAts.respCode = "0";
                    retAts.respInfo = respJson.getString("retMsg");

                    String refundStatus = respJson.getString("refundStatus");
                    if ("00".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_SUCCESS;
                    } else if ("01".equalsIgnoreCase(tmpRespCode) || "01".equalsIgnoreCase(refundStatus) || "06".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_FAIL;
                    } else if ("02".equalsIgnoreCase(tmpRespCode) || "02".equalsIgnoreCase(refundStatus) || "03".equalsIgnoreCase(refundStatus) || "04".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_PAYING;
                    } else if ("05".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_CLOSED;
                    }
                    retAts.oriOppOrderNo = respJson.getString("mchRefundSn");
                    if (StringHelper.isNullOrEmpty(retAts.oriOppOrderNo)) {
                        retAts.oriOppOrderNo = respJson.getString("payTrsSn");
                    }
                    retAts.oppRefundNo = respJson.getString("refundTrsSn");
                    retAts.originalCode = refundStatus;
                    retAts.originalInfo = "成功";
                    retAts.refundRecAccount = respJson.getString("cardNo");
                    retAts.originalCode = tmpRespCode;
                    retAts.originalInfo = respJson.getString("retMsg");
                    retAts.unifiedCode = getBankConfig().getMapValueByKey("recRefundMapping", tmpRespCode);
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "银行返回报文格式非法!";
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_reqQryRef.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }

    public RetAtsQryRecRefund queryRecRefundToday(ReqQryRecRefund p_reqQryRef) throws Exception {
        RetAtsQryRecRefund retAts = new RetAtsQryRecRefund();
        retAts.oriRdSeq = p_reqQryRef.oriRdSeq;
        retAts.refundNo = p_reqQryRef.refundNo;

        try {
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_reqQryRef.ownAct, "unifiedorder");
            if (actItem != null) {
                JSONObject refundRequest = new JSONObject();
                refundRequest.put("businessCode", "DR2003002");
                refundRequest.put("mchRefundSn", p_reqQryRef.refundNo);

                WriteBankLogStep2(refundRequest.toJSONString());
                String tmpStrRet = sendToBank("QRPayRefundQueryToday", refundRequest, actItem);
                WriteBankLogStep3(tmpStrRet);

                if (tmpStrRet.contains("{")) {
                    JSONObject respJson = JSON.parseObject(tmpStrRet);
                    String tmpRespCode = respJson.getString("retCode");
                    retAts.respCode = "0";
                    retAts.respInfo = respJson.getString("retMsg");

                    String refundStatus = respJson.getString("refundStatus");
                    if ("00".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_SUCCESS;
                    } else if ("01".equalsIgnoreCase(tmpRespCode) || "01".equalsIgnoreCase(refundStatus) || "06".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_FAIL;
                    } else if ("02".equalsIgnoreCase(refundStatus) || "03".equalsIgnoreCase(refundStatus) || "04".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_PAYING;
                    } else if ("05".equalsIgnoreCase(refundStatus)) {
                        retAts.refundState = RefundState.REFUND_CLOSED;
                    }
                    retAts.oriOppOrderNo = respJson.getString("mchRefundSn");
                    if (StringHelper.isNullOrEmpty(retAts.oriOppOrderNo)) {
                        retAts.oriOppOrderNo = respJson.getString("payTrsSn");
                    }
                    retAts.oppRefundNo = respJson.getString("refundTrsSn");
                    retAts.originalCode = refundStatus;
                    retAts.originalInfo = "成功";
                    retAts.refundRecAccount = respJson.getString("cardNo");
                    retAts.originalCode = tmpRespCode;
                    retAts.originalInfo = respJson.getString("retMsg");
                    retAts.unifiedCode = getBankConfig().getMapValueByKey("recRefundMapping", tmpRespCode);
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "银行返回报文格式非法!";
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_reqQryRef.ownAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        return retAts;
    }
}